package recursion;
//递归:方法直接或间接调用自己

//求阶乘
public class RecursionDemo {
    public static void main(String[] args) {
        int sum=getByLooper(5);
        int sum2=getByRecursion(3);
        int sum3=getSumByLooper(100);
        int sum4=getSumByRecursion(100);
        System.out.println(sum+"  "+sum2+" "+sum3+" "+sum4);
    }
    //lopper
    public static int getByLooper(int num){
        int sum=1;
        for (int i = 1; i <=num ; i++) {
            sum*=i;
        }
        return sum;
    }
    public static int getSumByLooper(int num){
        int sum=0;
        for (int i = 1; i <=num ; i++) {
            sum+=i;
        }
        return sum;
    }
    //recursion
    public static int getByRecursion(int num){
        if (num==1){
            return 1;
        }else{
            return   num*getByRecursion(num-1);
        }
    }
    public static int getSumByRecursion(int num){
        if (num==1){
            return 1;
        }else {
            return num+getSumByRecursion(num-1);
        }
    }
}
